﻿using AdvancedCollections.TreeHelper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AdvancedCollections
{
    public class AVLTree<T> : BinaryTree<T>, ISet<T>
    {
        public AVLTree()
            : base()
        {
        }

        public AVLTree(IComparer<T> comparer)
            : base(comparer)
        {
        }

        protected override BinaryTreeNode<T> CreateRoot()
        {
            return new AVLTreeNode<T>(null, _comparer);
        }

        protected override bool InternalAdd(T item)
        {
            var avlRoot = (AVLTreeNode<T>)_root;
            bool added = false;

            added = avlRoot.Add(item, ref avlRoot);

            if (added)
            {
                Count++;
                _root = avlRoot;
            }

            return added;
        }
    }
}